package com.mdd.service.agreement.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayUserAgreementPageSignModel;
import com.alipay.api.domain.AlipayUserAgreementUnsignModel;
import com.alipay.api.request.AlipayUserAgreementPageSignRequest;
import com.alipay.api.request.AlipayUserAgreementQueryRequest;
import com.alipay.api.request.AlipayUserAgreementUnsignRequest;
import com.alipay.api.response.AlipayUserAgreementPageSignResponse;
import com.alipay.api.response.AlipayUserAgreementQueryResponse;
import com.alipay.api.response.AlipayUserAgreementUnsignResponse;
import lombok.extern.slf4j.Slf4j;
import com.mdd.service.agreement.UserAgreementService;
import com.mdd.validate.agreement.AgreementUnsignInfo;
import com.mdd.validate.agreement.UserAgreementQuery;
import org.springframework.stereotype.Service;

/**
 * 用户代扣签约/解约操作
 * <p>
 * {@code @Description}
 * @Author 幻速
 * {@code @Date} 2023/10/31 14:15
 **/
@Slf4j
@Service
public class UserAgreementServiceImpl implements UserAgreementService {
    @Override
    public AlipayUserAgreementPageSignResponse userAgreementSign(AlipayClient alipayClient,AlipayUserAgreementPageSignModel model,String notifyUrl) throws AlipayApiException {
        log.info("AlipayUserAgreementPageSignRequest----->"+ JSON.toJSONString(model));
        AlipayUserAgreementPageSignRequest request = new AlipayUserAgreementPageSignRequest();
        request.setNotifyUrl(notifyUrl);
        request.setBizModel(model);
        request.setApiVersion("1");
        AlipayUserAgreementPageSignResponse response = alipayClient.sdkExecute(request);
        log.info("AlipayUserAgreementPageSignResponse----->"+JSON.toJSONString(response));
        return response;
    }

    @Override
    public AlipayUserAgreementUnsignResponse userAgreementUnsign(AlipayClient alipayClient, AlipayUserAgreementUnsignModel agreementUnsignInfo) throws AlipayApiException {
        log.info("AlipayUserAgreementUnsignRequest----->"+JSON.toJSONString(agreementUnsignInfo));
        AlipayUserAgreementUnsignRequest request = new AlipayUserAgreementUnsignRequest();
        request.setBizModel(agreementUnsignInfo);
        AlipayUserAgreementUnsignResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayUserAgreementUnsignResponse----->"+JSON.toJSONString(response));
        return response;
    }
    @Override
    public AlipayUserAgreementUnsignResponse userAgreementUnsign(AlipayClient alipayClient, JSONObject bizContent) throws AlipayApiException {
        log.info("AlipayUserAgreementUnsignRequest----->"+JSON.toJSONString(bizContent));
        AlipayUserAgreementUnsignRequest request = new AlipayUserAgreementUnsignRequest();
        request.setBizContent(bizContent.toJSONString());
        AlipayUserAgreementUnsignResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayUserAgreementUnsignResponse----->"+JSON.toJSONString(response));
        return response;
    }

    @Override
    public AlipayUserAgreementQueryResponse userAgreementQuery(AlipayClient alipayClient, UserAgreementQuery userAgreementQuery) throws AlipayApiException {
        log.info("AlipayUserAgreementQueryRequest------>"+JSON.toJSONString(userAgreementQuery));
        AlipayUserAgreementQueryRequest request = new AlipayUserAgreementQueryRequest();
        request.setBizContent(JSON.toJSONString(userAgreementQuery));
        AlipayUserAgreementQueryResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayUserAgreementQueryResponse---->"+response);
        return response;
    }
}
